import Systemdemo, {
  fill_cell, get_active_sheet_name,
  get_cell_value,
  get_position_of_para,
  get_row_from_pos_string, get_sheet_by_name,
  getLastColumnIndexOfSheet,
  set_cell_color,
  set_cells,
  set_position_of_para, showTaskPane
} from './systemdemo'
import Enum, { make_equal } from './enum'
import Settings, { 初始化抽汽信息 } from './settings'
import lodash from 'lodash'
import systemdemo from './systemdemo'
import Util from './util.js'

let app = window.Application

export function 低压缸(row) {
  // 当过热减温水从主给水后抽出，并喷入主蒸汽流量和温度压力测点之前时，过热减温水相当于过热系统内，不用考虑
  let 低压缸抽汽 = JSON.parse(wps.PluginStorage.getItem('低压缸抽汽'))
  let data = [
    ['低压缸', '低压缸进汽压力', 'MPa'], //+2
    ['', '低压缸进汽温度', '℃'],
    ['', '低压缸进汽焓', 'kJ/kg'],
    ['', '低压缸进汽熵', 'kJ/kg'],
    ['', '低压缸进汽流量', 't/h'],
    ['', '低压缸排汽流量', 't/h'],
    ['', '低压缸轴功率', 'MW']]
  let data1
  if (lodash.isEqual(低压缸抽汽, [6, 7, 8, 9])) { // 北方胜利
    data1 = [
      ['', '六段抽汽流量', 't/h'], // +8
      ['', '七段抽汽流量', 't/h'],
      ['', '八段抽汽流量', 't/h'],
      ['', '九段抽汽流量', 't/h'],
      ['', '六段抽汽能量', 'MJ/h'], // +8
      ['', '七段抽汽能量', 'MJ/h'],
      ['', '八段抽汽能量', 'MJ/h'],
      ['', '九段抽汽能量', 'MJ/h']]
  } else if (lodash.isEqual(低压缸抽汽, [6, 7, 8])) { // 陕投商洛
    data1 = [
      ['', '六段抽汽流量', 't/h'], // +8
      ['', '七段抽汽流量', 't/h'],
      ['', '八段抽汽流量', 't/h'],
      ['', '六段抽汽能量', 'MJ/h'], // +8
      ['', '七段抽汽能量', 'MJ/h'],
      ['', '八段抽汽能量', 'MJ/h']]
  }
  let data2 = [
    ['', '低压缸排汽压力', 'kPa'], // +12
    ['', '低压缸等熵排汽焓', 'kJ/kg'],
    ['', '低压缸排汽比容', 'm3/kg'],
    ['', '低压缸排汽焓', 'kJ/kg'],
    ['假设值', '膨胀线效率(ELEP)', '%'],
    ['计算值', '膨胀线效率(UEEP)', '%'],
    ['', '低压缸效率', '%'],
    ['', '低压缸排汽体积流量', 'm3/h'],
    ['', '低压缸排汽截面流速', 'm/s'],
    ['', '排汽损失', 'kJ/kg']
  ]
  data.push(...data1) // 拼接data和data2两个数组，es6语法
  data.push(...data2)
  let sp = Enum.StandardPoint
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  for (let i = 4; i <= getLastColumnIndexOfSheet("数据整理"); i++) {
    fill_cell(row + 2, i, 'para', 'inlet_low.P', 'action', sp)
    fill_cell(row + 3, i, 'para', 'inlet_low.T', 'action', sp)
    fill_cell(row + 4, i, 'formula', '{inlet_low.H}=if97.H_PT({row-2},{row-1})', undefined, sp)
    fill_cell(row + 5, i, 'formula', '{inlet_low.S}=if97.S_PT({row-3},{row-2})', undefined, sp)
    fill_cell(row + 6, i, 'formula', '{inlet_low.F}={turbine_medium.四抽至中排段流量}-{extract_5.F}-{leakage.中压后轴封漏汽量}', undefined, sp)

    fill_cell(row + 8, i, 'formula', '{turbine_low.shaft_power}={generator.power}/({generator.eta}/100)-{turbine_high.shaft_power}-{turbine_medium.shaft_power}', undefined, sp)

    let start_row1 = 0
    if (lodash.isEqual(低压缸抽汽, [6, 7, 8, 9])) { // 北方胜利
      fill_cell(row + 7, i, 'formula', '{outlet_low.F}={row-1}-{row+2}-{row+3}-{row+4}-{row+5}', undefined, sp) // 因为该行也与低压抽汽级数相关，因此放在if语句块中执行
      fill_cell(row + 9, i, 'para', 'extract_6.F', undefined, sp)
      fill_cell(row + 10, i, 'para', 'extract_7.F', undefined, sp)
      fill_cell(row + 11, i, 'para', 'extract_8.F', 'watch', sp)
      fill_cell(row + 12, i, 'para', 'extract_9.F', 'watch', sp)
      fill_cell(row + 13, i, 'para', 'extract_6.E', undefined, sp)
      fill_cell(row + 14, i, 'para', 'extract_7.E', undefined, sp)
      fill_cell(row + 15, i, 'para', 'extract_8.E', 'watch', sp)
      fill_cell(row + 16, i, 'para', 'extract_9.E', 'watch', sp)
      fill_cell(row + 17, i, 'para', 'outlet_low.P', undefined, sp)
      let p = get_cell_value(row + 17, i)
      if (p < 0.1) { // 如果背压小于50，说明当前背压单位是MPa
        fill_cell(row + 18, i, 'formula', '{outlet_low.H_等熵}=if97.H_PS({row-1},{inlet_low.S})', undefined, sp)
        fill_cell(row + 19, i, 'formula', '{outlet_low.sv}=V_PH({row-2},{row+1})', undefined, sp)
      } else {
        fill_cell(row + 18, i, 'formula', '{outlet_low.H_等熵}=if97.H_PS({row-1}/1000,{inlet_low.S})', undefined, sp)
        fill_cell(row + 19, i, 'formula', '{outlet_low.sv}=V_PH({row-2}/1000,{row+1})', undefined, sp)
      }
      fill_cell(row + 20, i, 'formula', '{outlet_low.H}=({inlet_low.F}*{inlet_low.H}-{row-7}-{row-6}-{row-5}-{row-4}-{turbine_low.shaft_power}*3600)/{outlet_low.F}', undefined, sp)
      start_row1 = row + 20
    } else if (lodash.isEqual(低压缸抽汽, [6, 7, 8])) { // 陕投商洛
      fill_cell(row + 7, i, 'formula', '{outlet_low.F}={row-1}-{row+2}-{row+3}-{row+4}', undefined, sp)
      fill_cell(row + 9, i, 'para', 'extract_6.F', undefined, sp)
      fill_cell(row + 10, i, 'para', 'extract_7.F', 'watch', sp)
      fill_cell(row + 11, i, 'para', 'extract_8.F', 'watch', sp)
      fill_cell(row + 12, i, 'para', 'extract_6.E', undefined, sp)
      fill_cell(row + 13, i, 'para', 'extract_7.E', 'watch', sp)
      fill_cell(row + 14, i, 'para', 'extract_8.E', 'watch', sp)
      fill_cell(row + 15, i, 'para', 'outlet_low.P', undefined, sp)
      let p = get_cell_value(row + 15, i)
      if (p < 0.1) { // 如果背压小于50，说明当前背压单位是MPa
        fill_cell(row + 16, i, 'formula', '{outlet_low.H_等熵}=if97.H_PS({row-1},{inlet_low.S})', undefined, sp)
        fill_cell(row + 17, i, 'formula', '{outlet_low.sv}=V_PH({row-2},{row+1})', undefined, sp)
      } else {
        fill_cell(row + 16, i, 'formula', '{outlet_low.H_等熵}=if97.H_PS({row-1}/1000,{inlet_low.S})', undefined, sp)
        fill_cell(row + 17, i, 'formula', '{outlet_low.sv}=V_PH({row-2}/1000,{row+1})', undefined, sp)
      }
      fill_cell(row + 18, i, 'formula', '{outlet_low.H}=({inlet_low.F}*{inlet_low.H}-{row-6}-{row-5}-{row-4}-{turbine_low.shaft_power}*3600)/{outlet_low.F}', undefined, sp)
      start_row1 = row + 18
    }
    fill_cell(start_row1 + 1, i, 'formula', '{turbine_low.eta_膨胀线}=90', undefined, sp)
    fill_cell(start_row1 + 2, i, 'formula', '{turbine_low.eta_计算值}=({inlet_low.H}-{row-2})/({inlet_low.H}-{outlet_low.H_等熵})*100', undefined, sp)
    fill_cell(start_row1 + 3, i, 'formula', '{turbine_low.eta}=0', undefined, sp)
    fill_cell(start_row1 + 4, i, 'formula', '{outlet_low.F_V}={outlet_low.sv}*{outlet_low.F}*1000', undefined, sp)
    fill_cell(start_row1 + 5, i, 'formula', '{outlet_low.velocity}={row-1}/{turbine_low.area}/3600', undefined, sp)
    // fill_cell(start_row1+6,, i, "formula", "{turbine_low.loss_排汽}=xz({row-1}, {}, {})")

    let val1 = get_cell_value(start_row1 + 1, i)
    let val2 = get_cell_value(start_row1 + 2, i)
    let j = 0
    while (Math.abs(val1 - val2) > 0.0001 && j < 100) {
      Systemdemo.set_cell_value(start_row1 + 1, i, val2)
      app.CalculateFull()
      val1 = val2
      val2 = get_cell_value(start_row1 + 2, i)
    }
  }
}

export function 抽汽9(row) {
  // 判断9抽是否存在
  let 低加级数 = JSON.parse(wps.PluginStorage.getItem('低加级数'))
  if (!lodash.includes(低加级数, 9)) {
    return true
  }
  let data = [
    ['九段抽汽', '低压缸进汽焓', 'kJ/kg'],
    ['', '低压缸进汽熵', 'kJ/kg'],
    ['', '低压缸膨胀线效率', '%'],
    ['', '九段抽汽压力', 'MPa'],
    ['', '九段抽汽等熵焓', 'kJ/kg'],
    ['', '九段抽汽焓', 'MPa'], // +5
    ['', '9号低加进水压力', 'MPa'],
    ['', '9号低加进水温度', '℃'],
    ['', '9号低加进水焓值', 'kJ/kg'],
    ['', '9号低加出水压力', 'MPa'], //+11
    ['', '9号低加出水温度', '℃'],
    ['', '9号低加出水焓值', 'kJ/kg'],
    ['', '9号低加疏水温度', '℃'], //+14
    ['', '9号低加疏水焓值', 'kJ/kg'],
    ['', '九段抽汽流量', 't/h'], //+16
    ['', '九号低加进汽流量', 't/h'],
    ['', '九号低加疏水流量', 't/h'],
    ['', '流经9号低加的流量', 't/h'],
    ['', '上端差', '℃'],
    ['', '下端差', '℃'],
    ['', '温升', '℃'],
    ['', '九段抽汽能量', 'MJ/h']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  let cols = getLastColumnIndexOfSheet('数据整理')
  for (let i = 4; i <= cols; i++) {
    fill_cell(row + 2, i, 'para', 'inlet_low.H', 'watch',sp)
    fill_cell(row + 3, i, 'para', 'inlet_low.S', 'watch',sp)
    fill_cell(row + 4, i, 'para', 'turbine_low.eta_膨胀线', 'watch',sp)
    fill_cell(row + 5, i, 'para', 'extract_9.P', undefined, sp)
    fill_cell(row + 6, i, 'formula', '{extract_9.H_等熵}=if97.H_PS({row-1},{row-3})', undefined, sp)
    fill_cell(row + 7, i, 'formula', '{extract_9.H}={row-5}-({row-5}-{row-1})*{row-3}/100', undefined, sp)
    // ----------------------------低加进水-----------------------------------
    let water_in = get_position_of_para('heater_9_water_in.P', i)
    if (water_in) {
      fill_cell(row + 8, i, 'para', 'heater_9_water_in.P', undefined, sp)
    } else {
      fill_cell(row + 8, i, 'formula', '{heater_9_water_in.P}=({water_condense_pump_out.P}+{heater_8_water_in.P})/2', undefined, sp)
    }
    fill_cell(row + 9, i, 'para', 'heater_9_water_in.T', undefined, sp)
    fill_cell(row + 10, i, 'formula', '{heater_9_water_in.H}=if97.H_PT({row-2},{row-1})', undefined, sp)
    // ============================低加进水压力=====================================
    // ------------------------------------ 低加出水 -----------------------------------------
    fill_cell(row + 11, i, 'para', 'heater_9_water_out.P', undefined, sp)
    fill_cell(row + 12, i, 'para', 'heater_9_water_out.T', undefined, sp)
    fill_cell(row + 13, i, 'formula', '{heater_9_water_out.H}=if97.H_PT({row-2},{row-1})', undefined, sp)
    // =============================== 低加出水 =================================
    // -------------------------------- 疏水 -------------------------------------
    fill_cell(row + 14, i, 'para', 'heater_9_vapor_out.T', undefined, sp)
    fill_cell(row + 15, i, 'formula', '{heater_9_vapor_out.H}=HW_T({row-1})', undefined, sp)
    // ============================疏水=====================================
    // 疏水流量
    let sp = Enum.StandardPoint['heater_8']
    if (sp['drain_to'] === 'heater_9') { // 如果8号低加的疏水流入9号低加
      // 9段抽汽流量
      fill_cell(row + 16, i, 'formula', '{extract_9.F}={row+1}', undefined, sp) // 抽汽流量
      fill_cell(row + 17, i, 'formula', '{heater_9_vapor_in.F}=({row+2}*({heater_9_water_out.H}-{heater_9_water_in.H})-{heater_8_vapor_out.F}*({heater_8_vapor_out.H}-{heater_9_vapor_out.H}))/({extract_9.H}-{heater_9_vapor_out.H})', undefined, sp)// 进汽流量
      fill_cell(row + 18, i, 'formula', '{heater_9_vapor_out.F}={row-1}+{heater_8_vapor_out.F}', undefined, sp) // 疏水流量
    } else { // 否则
      // 9段抽汽流量
      fill_cell(row + 16, i, 'formula', '{extract_9.F}={row+1}', undefined, sp) // 抽汽流量
      fill_cell(row + 17, i, 'formula', '{heater_9_vapor_in.F}={row+2}*({heater_9_water_out.H}-{heater_9_water_in.H})/({extract_9.H}-{heater_9_vapor_out.H})')// 进汽流量
      fill_cell(row + 18, i, 'formula', '{heater_9_vapor_out.F}={row-1}', undefined, sp)// 疏水流量
    }
    // 流经高加的给水流量
    fill_cell(row + 19, i, 'formula', '{heater_9.F}={heater_8.F}', undefined, sp)
    fill_cell(row + 20, i, 'formula', '{heater_9.上端差}=T_P({row-15})-{row-8}', undefined, sp)
    fill_cell(row + 21, i, 'formula', '{heater_9.下端差}={row-7}-{heater_9_water_in.T}', undefined, sp)
    fill_cell(row + 22, i, 'formula', '{heater_9.温升}={row-10}-{heater_9_water_in.T}', undefined, sp)
    fill_cell(row + 23, i, 'formula', '{extract_9.E}={extract_9.F}*{extract_9.H}', undefined, sp)
  }
}


export function 抽汽8(row) {
  // 判断8抽是否是湿蒸汽，湿蒸汽则不计算
  let 低加级数 = JSON.parse(wps.PluginStorage.getItem('低加级数'))
  if (!lodash.includes(低加级数, 8)) {
    return true
  }
  let data = [
    ['八段抽汽', '低压缸进汽焓', 'kJ/kg'],
    ['', '低压缸进汽熵', 'kJ/kg'],
    ['', '低压缸膨胀线效率', '%'],
    ['', '八段抽汽压力', 'MPa'],
    ['', '八段抽汽等熵焓', 'kJ/kg'],
    ['', '八段抽汽焓', 'MPa'], // +5
    ['', '8号低加进水压力', 'MPa'],
    ['', '8号低加进水温度', '℃'],
    ['', '8号低加进水焓值', 'kJ/kg'],
    ['', '8号低加出水压力', 'MPa'], //+11
    ['', '8号低加出水温度', '℃'],
    ['', '8号低加出水焓值', 'kJ/kg'],
    ['', '8号低加疏水温度', '℃'], //+14
    ['', '8号低加疏水焓值', 'kJ/kg'],
    ['', '八段抽汽流量', 't/h'], //+16
    ['', '八号低加进汽流量', 't/h'],
    ['', '八号低加疏水流量', 't/h'],
    ['', '流经8号低加的流量', 't/h'],
    ['', '上端差', '℃'],
    ['', '下端差', '℃'],
    ['', '温升', '℃'],
    ['', '八段抽汽能量', 'MJ/h']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  let cols = getLastColumnIndexOfSheet('数据整理')
  for (let i = 4; i <= cols; i++) {
    fill_cell(row + 2, i, 'para', 'inlet_low.H', 'watch')
    fill_cell(row + 3, i, 'para', 'inlet_low.S', 'watch')
    fill_cell(row + 4, i, 'para', 'turbine_low.eta_膨胀线', 'watch')
    fill_cell(row + 5, i, 'para', 'extract_8.P')
    fill_cell(row + 6, i, 'formula', '{extract_8.H_等熵}=if97.H_PS({row-1},{row-3})')
    fill_cell(row + 7, i, 'formula', '{extract_8.H}={row-5}-({row-5}-{row-1})*{row-3}/100')
    // ----------------------------低加进水-----------------------------------
    let water_in = get_position_of_para('heater_8_water_in.P', i)
    if (water_in) {
      fill_cell(row + 8, i, 'para', 'heater_8_water_in.P')
    } else {
      fill_cell(row + 8, i, 'formula', '{heater_8_water_in.P}=({water_condense_pump_out.P}+{heater_7_water_in.P})/2')
    }
    fill_cell(row + 9, i, 'formula', '{heater_8_water_in.T}=({seal_heater_water_out.T}+{heater_8_water_out.T})/2')
    fill_cell(row + 10, i, 'formula', '{heater_8_water_in.H}=if97.H_PT({row-2},{row-1})')
    // ============================低加进水压力=====================================
    // ------------------------------------ 低加出水 -----------------------------------------
    fill_cell(row + 11, i, 'para', 'heater_8_water_out.P')
    fill_cell(row + 12, i, 'para', 'heater_8_water_out.T')
    fill_cell(row + 13, i, 'formula', '{heater_8_water_out.H}=if97.H_PT({row-2},{row-1})')
    // =============================== 低加出水 =================================
    // -------------------------------- 疏水 -------------------------------------
    fill_cell(row + 14, i, 'para', 'heater_8_vapor_out.T')
    fill_cell(row + 15, i, 'formula', '{heater_8_vapor_out.H}=HW_T({row-1})')
    // ============================疏水=====================================
    // 疏水流量
    let sp = Enum.StandardPoint['heater_7']
    if (sp['drain_to'] === 'heater_8') { // 如果7号低加的疏水流入8号低加
      // 8段抽汽流量
      fill_cell(row + 16, i, 'formula', '{extract_8.F}={row+1}') // 抽汽流量
      fill_cell(row + 17, i, 'formula', '{heater_8_vapor_in.F}=({row+2}*({heater_8_water_out.H}-{heater_8_water_in.H})-{heater_7_vapor_out.F}*({heater_7_vapor_out.H}-{heater_8_vapor_out.H}))/({extract_8.H}-{heater_8_vapor_out.H})')// 进汽流量
      fill_cell(row + 18, i, 'formula', '{heater_8_vapor_out.F}={row-1}+{heater_7_vapor_out.F}') // 疏水流量
    } else if (sp['drain_to'] === 'heater_6_water_in') { // 如果7号低加疏水打入6号低加入口
      // 7段抽汽流量
      fill_cell(row + 16, i, 'formula', '{extract_8.F}={row+1}') // 抽汽流量
      fill_cell(row + 17, i, 'formula', '{heater_8_vapor_in.F}={row+2}*({heater_8_water_out.H}-{heater_8_water_in.H})/({extract_8.H}-{heater_8_vapor_out.H})')// 进汽流量
      fill_cell(row + 18, i, 'formula', '{heater_8_vapor_out.F}={row-1}')// 疏水流量
    }
    // 流经高加的给水流量
    fill_cell(row + 19, i, 'formula', '{heater_8.F}={heater_7.F}')
    fill_cell(row + 20, i, 'formula', '{heater_8.上端差}=T_P({row-15})-{row-8}')
    fill_cell(row + 21, i, 'formula', '{heater_8.下端差}={row-7}-{heater_8_water_in.T}')
    fill_cell(row + 22, i, 'formula', '{heater_8.温升}={row-10}-{heater_8_water_in.T}')
    fill_cell(row + 23, i, 'formula', '{extract_8.E}={extract_8.F}*{extract_8.H}')
  }
}

export function 热耗率(row) {
  // 当过热减温水从主给水后抽出，并喷入主蒸汽流量和温度压力测点之前时，过热减温水相当于过热系统内，不用考虑
  let data = [
    ['试验热耗率', '最终给水焓值', 'kJ/kg'],
    ['', '主蒸汽焓值', 'kJ/kg'],
    ['', '主蒸汽流量', 'kJ/kg'],
    ['', '冷再热蒸汽焓值', 'kJ/kg'],
    ['', '热再热蒸汽焓值', 'kJ/kg'],
    ['', '冷再热蒸汽流量', 'kJ/kg'],
    ['', '再热减温水压力', 'MPa'],
    ['', '再热减温水温度', 'MPa'],
    ['', '再热减温水焓值', 'kJ/kg'],
    ['', '再热减温水流量', 'kJ/kg'],
    ['', '机组净功率', 'MW'],
    ['', '试验热耗率', 'kJ/(kW.h)'] // 锅炉中的吸热量/机组净功率
  ]
  let sp = Enum.StandardPoint
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  for (let i = 4; i <= getLastColumnIndexOfSheet("数据整理"); i++) {
    fill_cell(row + 2, i, 'para', 'final_feed_water.H')
    fill_cell(row + 3, i, 'para', 'main_steam.H')
    fill_cell(row + 4, i, 'para', 'main_steam.F')
    // ------------------------------- 高加进汽 ---------------------------------
    fill_cell(row + 5, i, 'para', 'cold_reheat_steam.H')
    fill_cell(row + 6, i, 'para', 'inlet_medium.H')
    fill_cell(row + 7, i, 'para', 'cold_reheat_steam.F')
    // =============================== 高加进汽 ====================================
    // ----------------------------再热减温水-----------------------------------
    fill_cell(row + 8, i, 'para', 'water_reheater_reducing.P')
    fill_cell(row + 9, i, 'para', 'water_reheater_reducing.T')
    fill_cell(row + 10, i, 'formula', '{water_reheater_reducing.H}=if97.H_PT({row-2},{row-1})')
    fill_cell(row + 11, i, 'para', 'water_reheater_reducing.F')
    // ============================再热减温水=====================================
    // ---------------------------------机组净功率------------------------------------
    if ((sp.excitation.href.power) && (sp.excitation.href.power.length > 0)) { // 励磁功率存在且有值
      fill_cell(row + 12, i, 'formula', '{group.net_power}={generator.power}-{excitation.power}')
    } else {
      fill_cell(row + 12, i, 'formula', '{group.net_power}={generator.power}-{excitation.U}*{excitation.I}')
    }

    fill_cell(row + 13, i, 'formula', '{group.hr}=({row-9}*({row-10}-{row-11})+{row-6}*({row-7}-{row-8})+{row-2}*({row-7}-{row-3}))/{row-1}')
  }
}

export function get_link_of_extract(point_id) {
  /**
   * 根据enum.StandardPoint中的点名获取抽汽的连接信息，即抽汽从哪里抽，去到哪里去。
   * 例如：get_link_of_extract("extract_1") = ["turbine_high", "heater_1_vapor_in"]表示一抽从高压缸抽汽，至一号高加进汽。
   *
   * 需要注意的是，返回值可能是多个，如
   * get_link_of_extract("extract_4")= ["turbine_medium", ["deaerator_vapor_in", "pump_turbine_vapor_in"]]
   *
   * from 可取值为："turbine_high"，"turbine_medium"，"turbine_low"，"outlet_high"，"outlet_medium"
   * @param point_id - 取值为enum.StandardTestPoint中的测点名，例如"extract_1"
   */
  let io = Enum.StandardPoint[point_id]['io']
  if (!io || io['from'] === undefined) {
    console.log('未设置' + Enum.StandardPoint[point_id]['name'] + '的连接信息，尝试读取已有项目信息或加载默认设置！')
    初始化抽汽信息() // 加载pro.dat工作簿中的抽汽信息或生成默认的初始化抽汽信息
    io = Enum.StandardPoint[point_id]['io']
    console.log(Enum.StandardPoint)
  }

  if (io && io['from'] !== undefined) {
    let from = io['from']
    let to = io['to']
    if (from === '高压缸') {
      from = 'turbine_high'
    } else if (from === '中压缸') {
      from = 'turbine_medium'
    } else if (from === '低压缸') {
      from = 'turbine_low'
    } else if (from === '高压缸排汽') {
      from = 'outlet_high'
    } else if (from === '中压缸排汽') {
      from = 'outlet_medium'
    }

    if (to === '一号高加') {
      to = 'heater_1_vapor_in'
    } else if (to === '二号高加') {
      to = 'heater_2_vapor_in'
    } else if (to === '三号高加') {
      to = 'heater_3_vapor_in'
    } else if (to === '除氧器') {
      to = 'deaerator_vapor_in'
    } else if (to === '五号低加') {
      to = 'heater_5_vapor_in'
    } else if (to === '六号低加') {
      to = 'heater_6_vapor_in'
    } else if (to === '七号低加') {
      to = 'heater_7_vapor_in'
    } else if (to === '八号低加') {
      to = 'heater_8_vapor_in'
    } else if (to === '九号低加') {
      to = 'heater_9_vapor_in'
    } else if (lodash.isArray(to)) {
      let _to = []
      for (let comp of to) {
        if (comp === '除氧器') {
          _to.push('deaerator_vapor_in')
        } else if (comp === '给水泵汽轮机') {
          _to.push('pump_turbine_vapor_in')
        }
      }
      to = _to
    }
    return [from, to]
  } else {
    console.warn('尝试读取已有项目信息或加载默认设置失败！')
    return null
  }
}

export function init_turbine() {
  //
  // 初始化汽机的相关信息。包括
  // 1. 抽汽来源及去向；
  // 2. 指定哪些测点的替代测点（make_equal）:
  //   2.1 最终给水等于最高级高加出水
  //   2.2 给泵出水等于最低级高加进水，除了流量
  //   2.3 高排等于冷再，除了流量
  // 3. 初始化漏气信息；
  //   根据“漏气”工作簿初始化漏气信息
  //
  let active_sheet = app.ActiveSheet
  let 高加级数 = get_高加级数()
  let 低加级数 = get_低加级数()
  let 低加起始级数 = 低加级数[0]
  let 低加结束级数 = 低加级数[1]
  let _1_from_to_ = get_link_of_extract('extract_1')
  let to1 = _1_from_to_[1]
  if (to1 === 'heater_1_vapor_in') {
    to1 = 1
    wps.PluginStorage.setItem('heater_order', '1')
  } else if (to1 === 'heater_2_vapor_in') {
    to1 = 2 // 中煤昔阳安平发电厂的一抽进的是二号高加，这种电厂设计就很奇葩
    wps.PluginStorage.setItem('heater_order', '2')
  } else if (to1 === 'heater_3_vapor_in') {
    wps.PluginStorage.setItem('heater_order', '3')
    to1 = 3 // 中煤昔阳安平发电厂的一抽进的是二号高加，这种电厂设计就很奇葩
  } else if (to1 === 'heater_4_vapor_in') {
    wps.PluginStorage.setItem('heater_order', '4')
    to1 = 4
  }

  let _n_from_to_ = get_link_of_extract('extract_' + 高加级数)
  let ton = _n_from_to_[1]
  if (lodash.includes(ton, '1_vapor_in')) {  // 因为可能存在蒸冷器，因此不能用===进行判断
    ton = 1
  } else if (lodash.includes(ton, '2_vapor_in')) {
    ton = 2 // 中煤昔阳安平发电厂的一抽进的是二号高加，这种电厂设计就很奇葩
  } else if (lodash.includes(ton, '3_vapor_in')) {
    ton = 3 // 中煤昔阳安平发电厂的一抽进的是二号高加，这种电厂设计就很奇葩
  } else if (lodash.includes(ton, '4_vapor_in')) {
    ton = 4
  }

  make_equal('final_feed_water', 'heater_' + to1 + '_water_out') // 最终给水等于最高级高加出水
  make_equal('feed_pump_out', 'heater_' + ton + '_water_in') // 给泵出水等于最低级高加进水，除了流量
  make_equal('cold_reheat_steam', 'outlet_high') // 高排等于冷再，除了流量
  make_equal('deaerator_water_in', 'main_condense_water') // 除氧器进水=主凝结水
  if (wps.PluginStorage.getItem('heater_order') === '1') {
    make_equal('deaerator_drain_water_in', 'heater_' + get_高加级数() + '_vapor_out')
  } else {
    make_equal('deaerator_drain_water_in', 'heater_1_vapor_out')
  }
  if (Enum.StandardPoint.pump_before_water_in['exists']) {
    make_equal('deaerator_water_out', 'pump_before_water_in')
    make_equal('pump_before_water_out', 'feed_pump_in')
  } else {
    make_equal('deaerator_water_out', 'feed_pump_in')
  }

  // 如果存在蒸冷器，对蒸冷器的相关测点指定代替测点
  if (Enum.StandardPoint.heater_pre_3['exists']) {
    make_equal('heater_pre_3_vapor_in', 'extract_3')
    make_equal('heater_pre_3_vapor_out', 'heater_3_vapor_in')
    if (wps.PluginStorage.getItem('heater_order') === '1') {
      make_equal('heater_pre_3_water_in', 'heater_1_water_out')
    } else {
      let idx = get_高加级数()
      make_equal('heater_pre_3_water_in', 'heater_' + idx + '_water_out')
    }
  }

  if (to1 < ton) { // 说明高加是正常顺序编号
    for (let i = to1; i < ton; i++) {
      make_equal('heater_' + i + '_water_in', 'heater_' + (i + 1) + '_water_out')
    }
    for (let i = 低加起始级数; i < 低加结束级数; i++) {
      make_equal('heater_' + i + '_water_in', 'heater_' + (i + 1) + '_water_out')
    }
  } else {
    for (let i = ton; i < to1; i++) {
      make_equal('heater_' + i + '_water_out', 'heater_' + (i + 1) + '_water_in')
    }
    for (let i = 低加起始级数; i < 低加结束级数; i++) {
      make_equal('heater_' + i + '_water_out', 'heater_' + (i + 1) + '_water_in')
    }
  }


  let sp = Enum.StandardPoint
  let _temp_ = []
  for (let i of [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) {
    if (sp['extract_' + i]['exists']) {
      _temp_.push(i)
    }
  }
  wps.PluginStorage.setItem('抽汽级数', JSON.stringify(_temp_))
  let 高压缸抽汽 = []
  let 中压缸抽汽 = []
  let 低压缸抽汽 = []
  let 高排抽汽 = []
  let 中排抽汽 = []

  for (let i of _temp_) {
    let _from_to_ = get_link_of_extract('extract_' + i)
    let from = _from_to_[0]
    let to = _from_to_[1]
    // "turbine_high"，"turbine_medium"，"turbine_low"，"outlet_high"，"outlet_medium"
    if (from === 'turbine_high') {
      高压缸抽汽.push(i)
    } else if (from === 'turbine_medium') {
      中压缸抽汽.push(i)
    } else if (from === 'turbine_low') {
      低压缸抽汽.push(i)
    } else if (from === 'outlet_high') {
      高排抽汽.push(i)
    } else if (from === 'outlet_medium') {
      中排抽汽.push(i)
    }

    if (lodash.isArray(to)) {
      if (lodash.includes(to, 'deaerator_vapor_in')) {
        make_equal('deaerator_vapor_in', 'extract_' + i)
        if (wps.PluginStorage.getItem('heater_order') === '1') {
          make_equal('deaerator_water_in', 'heater_' + (i + 1) + '_water_out')
        }
      } else if (lodash.includes(to, 'pump_turbine_vapor_in')) {
        make_equal('pump_turbine_vapor_in', 'extract_' + i)
      }
    } else {
      make_equal(to, 'extract_' + i)
    }

  }
  wps.PluginStorage.setItem('高压缸抽汽', JSON.stringify(高压缸抽汽))
  wps.PluginStorage.setItem('中压缸抽汽', JSON.stringify(中压缸抽汽))
  wps.PluginStorage.setItem('低压缸抽汽', JSON.stringify(低压缸抽汽))
  wps.PluginStorage.setItem('高排抽汽', JSON.stringify(高排抽汽))
  wps.PluginStorage.setItem('中排抽汽', JSON.stringify(中排抽汽))
  let cols = getLastColumnIndexOfSheet('数据整理')
  wps.PluginStorage.setItem('sheet_columns', cols)
  Settings.初始化疏水信息()
  Settings.初始化漏汽信息()
  active_sheet.Activate()
}

export function make_equals(sp = Enum.StandardPoint) {
  make_equal('cold_reheat_steam', 'outlet_high', sp) // 高排等于冷再，除了流量
  make_equal('deaerator_water_in', 'main_condense_water', sp) // 除氧器进水=主凝结水

  if (sp.pump_before_water_in['exists']) {
    make_equal('deaerator_water_out', 'pump_before_water_in', sp)
    make_equal('pump_before_water_out', 'feed_pump_in', sp)
  } else {
    make_equal('deaerator_water_out', 'feed_pump_in', sp)
  }
  make_equal('env', 'gas_turbine_in', sp)
}

export function refresh_position_of_sheet(sheet, start_row = 0) {
  /**
   * 因为excel中删除行后，插件中StandPoint变量记录的变量位置并不会自动更新，使用该方法刷新一次变脸的位置。
   *
   * @param start_row: 从哪一行开始扫描更新，默认扫描全表，则start_row=0
   */
  let section
  let leakage = Enum.StandardPoint.leakage
  leakage.href = null // 清空之前的引用结果
  let rows = Systemdemo.getLastRowIndexOfSheet(sheet)
  for (let row = start_row; row <= rows; row++) {
    let _ = get_cell_value(row, 1, sheet)
    if (_ && _ !== '') { // 如果_存在且不为空字符串
      section = _
    }
    let var_name = get_cell_value(row, 2, sheet)
    try {
      let var_id = Enum.name_alias[section][var_name]
      if (var_id) {
        set_position_of_para(var_id, sheet + '!{Y}' + row)
      }
      console.log(Enum.name_alias[section])
    } catch (e) {
    }
  }
}


export function 中压缸效率(row) {
  /**
   * 在“汽机"页面中写入高压缸计算结果区域
   * @type {string[][]}
   */
  let data = [
    ['中压缸', '再热蒸汽压力', 'MPa'],
    ['', '再热蒸汽温度', '℃'],
    ['', '再热蒸汽焓值', 'kJ/kg'],
    ['', '再热蒸汽熵值', 'kJ/(kgK)'],
    ['', '中排压力', 'MPa'],
    ['', '中排温度', '℃'],
    ['', '中排焓值', 'kJ/kg'],
    ['', '中压缸效率', '%']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  // testPoints = SystemDemo.refreshTestPoints();
  let cols = getLastColumnIndexOfSheet('数据整理')
  let sp = Enum.StandardPoint
  for (let i = 4; i <= cols; i++) {
    // 工况列从第4(D)列开始
    fill_cell(row + 2, i, 'para', 'inlet_medium.P')
    fill_cell(row + 3, i, 'para', 'inlet_medium.T')
    fill_cell(row + 4, i, 'formula', '{inlet_medium.H}=if97.H_PT({row-2}, {row-1})')
    fill_cell(row + 5, i, 'formula', '{inlet_medium.S}=if97.S_PT({row-3}, {row-2})')
    fill_cell(row + 6, i, 'para', 'outlet_medium.P')
    fill_cell(row + 7, i, 'para', 'outlet_medium.T')
    fill_cell(row + 8, i, 'formula', '{outlet_medium.H}=if97.H_PT({row-2}, {row-1})')
    fill_cell(row + 9, i, 'formula', '{turbine_medium.eta}=({row-5}-{row-1})/({row-5}-if97.H_PS({row-3},{row-4}))*100')
  }
}

export function 不明漏量(row) {
  /**
   * 在“汽机"页面中写入高压缸计算结果区域
   * @type {string[][]}
   */
  let data = [
    ['不明泄露', '工况持续时间', 'h'],
    ['', '除氧器开始水位', 'mm'],
    ['', '除氧器结束水位', 'mm'],
    ['', '除氧器开始容积', 'm3'],
    ['', '除氧器结束容积', 'm3'],
    ['', '除氧器出水温度', '℃'],
    ['', '除氧器当量流量', 't/h'],
    ['', '', ''],
    ['', '凝汽器开始水位', 'mm'],
    ['', '凝汽器结束水位', 'mm'],
    ['', '凝汽器开始容积', 'm3'],
    ['', '凝汽器结束容积', 'm3'],
    ['', '热井出水温度', '℃'],
    ['', '凝汽器当量流量', 't/h'],
    ['', '', ''],
    ['', '明漏量', 't/h'],
    ['', '不明漏量', 't/h'],
    ['', '主蒸汽流量', 't/h'],
    ['', '不明漏率', '%']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  let cols = getLastColumnIndexOfSheet('数据整理')
  for (let i = 4; i <= cols; i++) {
    fill_cell(row + 2, i, 'formula', '{datetime.持续时间}=({datetime.end}-{datetime.start})*24')
    fill_cell(row + 3, i, 'para', 'deaerator_water_lvl.start')
    fill_cell(row + 4, i, 'para', 'deaerator_water_lvl.end')
    fill_cell(row + 5, i, 'formula', '{deaerator_water_lvl.start_volume}=Cyqv({row-2})')
    fill_cell(row + 6, i, 'formula', '{deaerator_water_lvl.end_volume}=Cyqv({row-2})')
    fill_cell(row + 7, i, 'para', 'deaerator_water_out.T')
    fill_cell(row + 8, i, 'formula', '{deaerator_water_lvl.as_F}=({row-3}-{row-2})/VW_T({row-1})/1000/{row-6}')
    fill_cell(row + 10, i, 'para', 'condenser_water_lvl.start')
    fill_cell(row + 11, i, 'para', 'condenser_water_lvl.end')
    fill_cell(row + 12, i, 'formula', '{condenser_water_lvl.start_volume}=Nqqv({row-2})')
    fill_cell(row + 13, i, 'formula', '{condenser_water_lvl.end_volume}=Nqqv({row-2})')
    fill_cell(row + 14, i, 'para', 'heat_well_out.T')
    fill_cell(row + 15, i, 'formula', '{condenser_water_lvl.as_F}=({row-3}-{row-2})/VW_T({row-1})/1000/{row-13}')

    if (Enum.StandardPoint.leakage_known.exists) {
      fill_cell(row + 17, i, 'para', 'leakage_known.F')
    } else {
      fill_cell(row + 17, i, 'formula', '{leakage_known.F}=0')
    }
    fill_cell(row + 18, i, 'formula', '{leakage_unknown.F}={row-10}+{row-3}-{row-1}')

    // 计算不明漏率，不明漏率与主蒸汽流量有关系，目前尚不知道主蒸汽流量，该条计算后续在完善
    fill_cell(row + 19, i, 'para', 'main_steam.F', 'watch') // 该方法不会生成main_steam.F的引用，因此只能使用{row-i}的方式引用该行
    fill_cell(row + 20, i, 'formula', '{leakage_unknown.rate}={row-2}/{row-1}')
  }
}

export function 给水(row) {
  let data = [
    ['给水', '给水泵出口压力', 'MPa'],
    ['', '最终给水压力', 'MPa'],
    ['', '最终给水温度', '℃'],
    ['', '最终给水焓值', 'kJ/kg'],
    ['上一步假设值', '最终给水流量', 't/h'],
    ['当前步计算值', '最终给水流量', 't/h'],
    ['', '炉侧不明泄漏量占比', '/'],
    ['', '炉侧不明泄露量', 't/h'],
    ['', '', ''],
    ['', '流经高加旁路的给水流量', 't/h'],
    ['', '流经高加旁路的给水焓值', 'kJ/kg'],
    ['', '流经高加给水流量', 't/h']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  set_cell_color(row + 6, 1, 6) // 蓝色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  let cols = getLastColumnIndexOfSheet('数据整理')
  for (let i = 4; i <= cols; i++) {
    fill_cell(row + 2, i, 'para', 'feed_pump_out.P')
    fill_cell(row + 3, i, 'para', 'final_feed_water.P')
    fill_cell(row + 4, i, 'para', 'final_feed_water.T')
    fill_cell(row + 5, i, 'formula', '{final_feed_water.H}=if97.H_PT({row-2}, {row-1})')
    fill_cell(row + 6, i, 'formula', '{final_feed_water.F}=1.1*{main_condense_water.F}')
    fill_cell(row + 7, i, 'para', 'final_feed_water.F_计算值', 'watch')

    let boiler = get_position_of_para('leakage_unknown.percentage_boiler', i)
    let row_num = get_row_from_pos_string(boiler)
    if (row_num === row + 8) {  // 如果当前参数引用的是当前行，则认为不存在当前参数
      boiler = null
    }
    if (boiler) {
      fill_cell(row + 8, i, 'para', 'leakage_unknown.percentage_boiler')
    } else {
      let turbine = get_position_of_para('leakage_unknown.percentage_turbine', i)
      if (turbine) {
        fill_cell(row + 8, i, 'para', 'leakage_unknown.percentage_turbine')
      } else {
        fill_cell(row + 8, i, 'formula', '{leakage_unknown.percentage_boiler}=0.5') // 默认按1:1分配炉侧和机侧的不明泄露量
      }
    }

    fill_cell(row + 9, i, 'formula', '{leakage_unknown.F_boiler}={leakage_unknown.F}*{row-1}/100')
    // row+9为空行
    // 设置流经高加旁路的给水流量，默认值为0

    fill_cell(row + 11, i, 'para', 'bypass_high_water.F', 0)
    fill_cell(row + 12, i, 'para', 'bypass_high_water.H', 0)
    fill_cell(row + 13, i, 'formula', '{heater_1.F}={final_feed_water.F}-{row-2}')
  }
}

export function 高压主蒸汽(row) {
  let data = [
    ['高压主蒸汽', '主蒸汽压力', 'MPa'],
    ['', '主蒸汽温度', '℃'],
    ['', '主蒸汽焓值', 'kJ/kg'],
    ['', '过热减温水流量', 't/h'],
    ['', '主蒸汽流量', 't/h']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  let cols = getLastColumnIndexOfSheet('数据整理')
  for (let i = 4; i <= cols; i++) {
    fill_cell(row + 2, i, 'para', 'main_steam.P')
    fill_cell(row + 3, i, 'para', 'main_steam.T')
    fill_cell(row + 4, i, 'formula', '{main_steam.H}=if97.H_PT({row-2}, {row-1})')
    fill_cell(row + 5, i, 'para', 'water_overheat_reducing.F')
    fill_cell(row + 6, i, 'formula', '{main_steam.F}={final_feed_water.F}-{leakage_unknown.F_boiler}+{row-1}')
  }
}

export function 有调节级() {
  return Enum.StandardPoint.adjust_steam['exist']
}

export function 调节级(row) {
  let data = [
    ['调节级', '调节级压力', 'MPa'],
    ['', '调节级温度', '℃'],
    ['', '调节级焓值', 'kJ/kg'],
    ['', '调节级效率', 't/h'],
    ['', '调节级后蒸汽流量', 't/h']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  for (let i = 4; i <= getLastColumnIndexOfSheet("数据整理"); i++) {
  }
}

function get_高加级数() {
  //
  // 获取热力系统中高加的级数，最大支持4级高加，不考虑0号高加
  // @type {number}
  //
  let x = 0
  let 高加级数 = []
  if (Enum.StandardPoint.heater.value.高加级数) {
    return Enum.StandardPoint.heater.value.高加级数
  } else {
    if (Enum.StandardPoint.heater_1_vapor_out.exists) {
      x = 1
      高加级数.push(1)
    }
    if (Enum.StandardPoint.heater_2_vapor_out.exists) {
      x = 2
      高加级数.push(2)
    }
    if (Enum.StandardPoint.heater_3_vapor_out.exists) {
      x = 3
      高加级数.push(3)
    }
    if (Enum.StandardPoint.heater_4_vapor_out.exists) {
      x = 4
      高加级数.push(4)
    }
  }
  Enum.StandardPoint.heater.value.高加级数 = x
  wps.PluginStorage.setItem('高加级数', JSON.stringify(高加级数))
  return x
}

function get_低加级数() {
  //
  // 获取热力系统中低加的级数，最大支持9级低加
  // @type {number}
  //
  let x = 0
  let x_end = 0
  let 低加级数 = []
  if (Enum.StandardPoint.heater.value.低加起始级数) {
    return [Enum.StandardPoint.heater.value.低加起始级数, Enum.StandardPoint.heater.value.低加结束级数]
  } else {
    if (Enum.StandardPoint.heater_5_vapor_out.exists) {
      x = 5
      x_end = 5
      低加级数.push(5)
    }
    if (Enum.StandardPoint.heater_6_vapor_out.exists) {
      if (x === 0) {
        x = 6
      }  // 如果起始低加编号为0则设置为6
      x_end = 6
      低加级数.push(6)
    }
    if (Enum.StandardPoint.heater_7_vapor_out.exists) {
      if (x === 0) {
        x = 7
      }
      x_end = 7
      低加级数.push(7)
    }
    if (Enum.StandardPoint.heater_8_vapor_out.exists) {
      if (x === 0) {
        x = 8
      }
      x_end = 8
      低加级数.push(8)
    }
    if (Enum.StandardPoint.heater_9_vapor_out.exists) {
      if (x === 0) {
        x = 9
      }
      x_end = 9
      低加级数.push(9)
    }
  }
  Enum.StandardPoint.heater.value.低加起始级数 = x
  Enum.StandardPoint.heater.value.低加结束级数 = x_end
  wps.PluginStorage.setItem('低加级数', JSON.stringify(低加级数))
  return [x, x_end]
}

export function 抽汽1(row) {
  let data = [
    ['一段抽汽', '一段抽汽压力', 'MPa'],
    ['', '一段抽汽温度', '℃'],
    ['', '一段抽汽焓值', 'kJ/kg']
  ]

  let _from_to_ = get_link_of_extract('extract_1')
  let to = _from_to_[1]
  let idx
  if (to === 'heater_1_vapor_in') {
    idx = 1
  } else if (to === 'heater_2_vapor_in') {
    idx = 2 // 中煤昔阳安平发电厂的一抽进的是二号高加，这种电厂设计就很奇葩
  }
  let data2 = [
    ['', idx + '号高加进汽压力', 'MPa'],
    ['', idx + '号高加进汽温度', '℃'],
    ['', idx + '号高加进汽焓值', 'kJ/kg'],
    ['', idx + '号高加进水压力', 'MPa'],
    ['', idx + '号高加进水温度', '℃'],
    ['', idx + '号高加进水焓值', 'kJ/kg'],
    ['', idx + '号高加出水压力', 'MPa'],
    ['', idx + '号高加出水温度', '℃'],
    ['', idx + '号高加出水焓值', 'kJ/kg'],
    ['', idx + '号高加疏水温度', '℃'],
    ['', idx + '号高加疏水焓值', 'kJ/kg'],
    ['', '一段抽汽流量', 't/h'],
    ['', idx + '号高加进汽流量', 't/h'],
    ['', idx + '号高加疏水流量', 't/h'],
    ['', '流经' + idx + '号高加给水流量', 't/h'],
    ['', '一段抽汽压损', '%'],
    ['', '上端差', '℃'],
    ['', '下端差', '℃'],
    ['', idx + '号高加温升', '℃'],
    ['', '一段抽汽能量', 'MJ/h']
  ]
  data.push(...data2) // 拼接data和data2两个数组，es6语法
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容

  for (let i = 4; i <= getLastColumnIndexOfSheet("数据整理"); i++) {
    fill_cell(row + 2, i, 'para', 'extract_1.P')
    fill_cell(row + 3, i, 'para', 'extract_1.T')
    fill_cell(row + 4, i, 'formula', '{extract_1.H}=if97.H_PT({row-2}, {row-1})')
    // ------------------------------- 高加进汽 ---------------------------------
    fill_cell(row + 5, i, 'para', to + '.P')
    fill_cell(row + 6, i, 'para', to + '.T')
    fill_cell(row + 7, i, 'formula', '{' + to + '.H}=if97.H_PT({row-2}, {row-1})')
    // =============================== 高加进汽 ====================================
    // ----------------------------高加进水-----------------------------------
    let point_id = 'heater_' + idx + '_water_in'
    let water_in_P = Enum.StandardPoint[point_id]['href']['P']
    if (water_in_P) {
      // 如果存在相应高加的进水压力，就使用
      fill_cell(row + 8, i, 'para', point_id + '.P')
    } else {
      let water_out_P = Enum.StandardPoint['heater_' + idx + '_water_out']['href']['P']
      if (!water_out_P) {
        water_out_P = Enum.StandardPoint.final_feed_water['href']['P']
        if (!water_out_P) {
          alert('未发现最终给水压力或其替代点的压力，请检查！')
          return null
        }
        set_position_of_para('heater_' + idx + '_water_out.P', water_out_P[0])
      }
    }
    let 高加级数 = get_高加级数()
    point_id = 'heater_' + idx + '_water_out.P'
    let f = '{heater_' + idx + '_water_in.P}={' + point_id + '}+({feed_pump_out.P}-{' + point_id + '})/' + 高加级数
    fill_cell(row + 8, i, 'formula', f)
    // ============================高加进水压力=====================================
    // ---------------------------------高加进水焓值------------------------------------
    try {
      fill_cell(row + 9, i, 'para', 'heater_' + idx + '_water_in.T')
    } catch (e) {
      console.log(e)
      alert('未找到' + idx + '号高加进水温度，请检查！')
      return null
    }
    fill_cell(row + 10, i, 'formula', '{heater_' + idx + '_water_in.H}=if97.H_PT({row-2},{row-1})')
    // ============================ 高加进水焓值 =====================================
    // ------------------------------------ 高加出水 -----------------------------------------
    fill_cell(row + 11, i, 'para', 'heater_' + idx + '_water_out.P')
    try {
      fill_cell(row + 12, i, 'para', 'heater_' + idx + '_water_out.T')
    } catch (e) {
      console.log(e)
      fill_cell(row + 12, i, 'formula', '{heater_' + idx + '_water_out.T}={final_feed_water.T}')
    }
    fill_cell(row + 13, i, 'formula', '{heater_' + idx + '_water_out.H}=if97.H_PT({row-2},{row-1})')
    // =============================== 高加出水 =================================
    // -------------------------------- 疏水 -------------------------------------
    fill_cell(row + 14, i, 'para', '{heater_' + idx + '_vapor_out.T}')
    fill_cell(row + 15, i, 'formula', '{heater_' + idx + '_vapor_out.H}=HW_T({row-1})') // 疏水焓值
    // ================================ 疏水 =====================================
    fill_cell(row + 16, i, 'formula', '{extract_1.F}={row+3}*({row-3}-{row-6})/({row-9}-{row-1})')
    fill_cell(row + 17, i, 'formula', '{heater_' + idx + '_vapor_in.F}={row-1}')
    fill_cell(row + 18, i, 'formula', '{heater_' + idx + '_vapor_out.F}={row-1}')
    fill_cell(row + 19, i, 'para', '{final_feed_water.F}')
    set_position_of_para('heater_' + idx + '_water_out.F', app.ActiveSheet.Name + '!{Y}' + row + 19)
    set_position_of_para('heater_' + idx + '.F', app.ActiveSheet.Name + '!{Y}' + row + 19)
    fill_cell(row + 20, i, 'formula', '{extract_1.压损}=({row-18}-{row-15})/{row-18}*100')
    fill_cell(row + 21, i, 'formula', '{heater_' + idx + '.上端差}=T_P({row-16})-{row-9}')
    fill_cell(row + 22, i, 'formula', '{heater_' + idx + '.下端差}={row-8}-{row-13}')
    fill_cell(row + 23, i, 'formula', '{heater_' + idx + '.温升}={row-11}-{row-14}')
    fill_cell(row + 24, i, 'formula', '{extract_1.E}={extract_1.F}*{extract_1.H}')
  }
}

export function 抽汽4(row) {
  let to4 = get_link_of_extract('extract_4')[1]
  let type4 = -1 // type4=1表示四抽进除氧器和小机，type4=2表示4抽只进除氧器
  if (lodash.isArray(to4)) {
    if (lodash.includes(to4, 'deaerator_vapor_in') && lodash.includes(to4, 'pump_turbine_vapor_in')) {
      type4 = 1
    }
  } else {
    if (to4 !== 'deaerator_vapor_in') {
      type4 = 2
      alert('暂不支持四抽不进除氧器和小机的机型')
      return false
    }
  }
  let data = [
    ['四段抽汽', '四段抽汽压力', 'MPa'],
    ['', '四段抽汽温度', '℃'],
    ['', '四段抽汽焓值', 'kJ/kg'],
    ['', '除氧器进汽压力', 'MPa'],
    ['', '除氧器进汽温度', '℃'],
    ['', '除氧器进汽焓值', 'kJ/kg'],
    ['', '除氧器进水压力', 'MPa'],
    ['', '除氧器进水温度', '℃'],
    ['', '除氧器进水焓值', 'kJ/kg'],
    ['', '除氧器出水压力', 'MPa'],
    ['', '除氧器出水温度', '℃'],
    ['', '除氧器出水焓值', 'kJ/kg'],
    ['', '高加疏水温度', '℃'],
    ['', '高加疏水焓值', 'kJ/kg'],
    ['', '高加疏水流量', 't/h'],
    ['', '主凝结水流量', 't/h'],
    ['', '四段抽汽流量', 't/h'],
    ['', '小机进汽流量', 't/h'],
    ['', '除氧器进汽流量', 't/h'],
    ['', '除氧器出水流量', 't/h'],
    ['', '最终给水流量', 't/h'],
    ['', '四段抽汽能量', 'MJ/h']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  let cols = getLastColumnIndexOfSheet('数据整理')
  for (let i = 4; i <= cols; i++) {
    fill_cell(row + 2, i, 'para', 'extract_4.P')
    fill_cell(row + 3, i, 'para', 'extract_4.T')
    fill_cell(row + 4, i, 'formula', '{extract_4.H}=if97.H_PT({row-2}, {row-1})')
    // ------------------------------- 除氧器进汽 ---------------------------------
    fill_cell(row + 5, i, 'para', 'deaerator_vapor_in.P')
    fill_cell(row + 6, i, 'para', 'deaerator_vapor_in.T')
    fill_cell(row + 7, i, 'formula', '{deaerator_vapor_in.H}=if97.H_PT({row-2},{row-1})')
    // =============================== 除氧器进汽 ====================================
    // ----------------------------除氧器进水-----------------------------------
    fill_cell(row + 8, i, 'para', 'deaerator_water_in.P')
    fill_cell(row + 9, i, 'para', 'deaerator_water_in.T')
    fill_cell(row + 10, i, 'formula', '{deaerator_water_in.H}=if97.H_PT({row-2},{row-1})')
    // ============================除氧器进水=====================================
    // ----------------------------除氧器出水-----------------------------------
    fill_cell(row + 11, i, 'para', 'deaerator_water_out.P')
    fill_cell(row + 12, i, 'para', 'deaerator_water_out.T')
    fill_cell(row + 13, i, 'formula', '{deaerator_water_out.H}=if97.H_PT({row-2},{row-1})')
    // ============================除氧器出水=====================================
    // 高加疏水
    fill_cell(row + 14, i, 'para', '{deaerator_drain_water_in.T}')
    fill_cell(row + 15, i, 'para', '{deaerator_drain_water_in.H}')
    fill_cell(row + 16, i, 'para', '{deaerator_drain_water_in.F}')
    fill_cell(row + 17, i, 'para', '{deaerator_water_in.F}')
    if (type4 === 1) {
      fill_cell(row + 18, i, 'formula', '{extract_4.F}={row+1}+{row+2}') // 四抽流量
      fill_cell(row + 19, i, 'para', 'pump_turbine_vapor_in.F') // 小机进汽流量
      fill_cell(row + 20, i, 'formula', '{deaerator_vapor_in.F}=(({row-3}+{row-4})*{row-7}-' +
        '{row-4}*{row-5}-{row-10}*{row-3})/({row-13}-{row-7})')
      fill_cell(row + 21, i, 'formula', '{deaerator_water_out.F}={row-1}+{row-4}+{row-5}')
      fill_cell(row + 22, i, 'formula',
        '{final_feed_water.F_计算值}={row-1}-{water_reheater_reducing.F}')
    }
    fill_cell(row + 23, i, 'formula', '{extract_4.E}={extract_4.F}*{extract_4.H}')

    // ----------------------------- 循环将最终给水的假设值设置为计算值，直至二者相等 --------------------------------
    let f_假设值_pos = get_position_of_para('final_feed_water.F', i, '汽机')
    let x_f_假设值 = get_row_from_pos_string(f_假设值_pos)
    let f_假设值 = get_cell_value(x_f_假设值, i, '汽机')
    let f_计算值 = get_cell_value(row + 22, i, '汽机')
    let j = 0
    while (Math.abs(f_假设值 - f_计算值) > 0.001 && j < 100) { // 当流量误差小于0.005t/h时，认为已经收敛
      Systemdemo.set_cell_value(x_f_假设值, i, f_计算值, '汽机')
      app.ActiveSheet.Calculate()
      f_假设值 = f_计算值
      f_计算值 = get_cell_value(row + 22, i, '汽机')
      j++
    }
  }
}

export function 抽汽5(row) {
  let data = [
    ['五段抽汽', '五段抽汽压力', 'MPa'], // +2
    ['', '五段抽汽温度', '℃'],
    ['', '五段抽汽焓值', 'kJ/kg'],
    ['', '5号低加进汽压力', 'MPa'], // +5
    ['', '5号低加进汽温度', '℃'],
    ['', '5号低加进汽焓值', 'kJ/kg'],
    ['', '凝泵出口压力', 'MPa'],
    ['', '5号低加进水压力', 'MPa'], //+8
    ['', '5号低加进水温度', '℃'],
    ['', '5号低加进水焓值', 'kJ/kg'],
    ['', '5号低加出水压力', 'MPa'], //+11
    ['', '5号低加出水温度', '℃'],
    ['', '5号低加出水焓值', 'kJ/kg'],
    ['', '5号低加疏水温度', '℃'], //+14
    ['', '5号低加疏水焓值', 'kJ/kg'],
    ['', '五段抽汽流量', 't/h'], //+16
    ['', '五号低加进汽流量', 't/h'],
    ['', '五号低加疏水流量', 't/h'],
    ['', '流经5号低加的流量', 't/h'],
    ['', '五段抽汽压损', '%'],
    ['', '上端差', '℃'],
    ['', '下端差', '℃'],
    ['', '温升', '℃'],
    ['', '五段抽汽能量', 'MJ/h']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  let cols = getLastColumnIndexOfSheet('数据整理')
  for (let i = 4; i <= cols; i++) {
    fill_cell(row + 2, i, 'para', 'extract_5.P')
    fill_cell(row + 3, i, 'para', 'extract_5.T')
    fill_cell(row + 4, i, 'formula', '{extract_5.H}=if97.H_PT({row-2}, {row-1})')
    // ------------------------------- 低加进汽 ---------------------------------
    fill_cell(row + 5, i, 'para', 'heater_5_vapor_in.P')
    fill_cell(row + 6, i, 'para', 'heater_5_vapor_in.T')
    fill_cell(row + 7, i, 'formula', '{heater_5_vapor_in.H}=if97.H_PT({row-2},{row-1})')
    // =============================== 低加进汽 ====================================
    // ----------------------------低加进水-----------------------------------
    // 查询凝泵出口压力测点，依次选取凝泵出口压力，凝泵A出口压力、凝泵B出口压力、主凝结水压力
    let pos = get_position_of_para('water_condense_pump_out.P', i)
    let tsp = ''
    if (pos) {
      tsp = 'water_condense_pump_out.P'
    } else {
      pos = get_position_of_para('water_condense_pump_A_out.P')
      if (pos) {
        tsp = 'water_condense_pump_A_out.P'
      } else {
        pos = get_position_of_para('water_condense_pump_B_out.P')
        if (pos) {
          tsp = 'water_condense_pump_B_out.P'
        } else {
          tsp = 'main_condense_water.P'
        }
      }
    }
    fill_cell(row + 8, i, 'formula', '{water_condense_pump_out.P}={' + tsp + '}')
    let 低加级数: any = get_低加级数()
    if (lodash.isArray(低加级数)) {
      低加级数 = 低加级数[1] - 低加级数[0] + 1
    }
    fill_cell(row + 9, i, 'formula', '{heater_5_water_in.P}={deaerator_water_in.P}' +
      '+({row-1}-{deaerator_water_in.P})/' + 低加级数)
    fill_cell(row + 10, i, 'para', 'heater_5_water_in.T')
    fill_cell(row + 11, i, 'formula', '{heater_5_water_in.H}=if97.H_PT({row-2},{row-1})')
    // ============================低加进水压力=====================================
    // ------------------------------------ 高加出水 -----------------------------------------
    fill_cell(row + 12, i, 'para', 'heater_5_water_out.P')
    fill_cell(row + 13, i, 'para', 'heater_5_water_out.T')
    fill_cell(row + 14, i, 'formula', '{heater_5_water_out.H}=if97.H_PT({row-2},{row-1})')
    // =============================== 高加出水 =================================
    // -------------------------------- 疏水 -------------------------------------
    fill_cell(row + 15, i, 'para', 'heater_5_vapor_out.T')
    fill_cell(row + 16, i, 'formula', '{heater_5_vapor_out.H}=HW_T({row-1})')
    // ============================疏水=====================================
    // 5段抽汽流量
    fill_cell(row + 17, i, 'formula', '{extract_5.F}={row+3}*({row-3}-{row-6})/({row-10}-{row-1})') // 抽汽流量
    fill_cell(row + 18, i, 'formula', '{heater_5_vapor_in.F}={row-1}')// 进汽流量
    // 疏水流量
    fill_cell(row + 19, i, 'formula', '{heater_5_vapor_out.F}={row-1}')
    // 流经高加的给水流量
    fill_cell(row + 20, i, 'formula', '{heater_5.F}={deaerator_water_in.F}')
    fill_cell(row + 21, i, 'formula', '{extract_5.压损}=({row-19}-{row-16})/{row-19}*100')
    fill_cell(row + 22, i, 'formula', '{heater_5.上端差}=T_P({row-17})-{row-9}')
    fill_cell(row + 23, i, 'formula', '{heater_5.下端差}={row-8}-{row-13}')
    fill_cell(row + 24, i, 'formula', '{heater_5.温升}={row-11}-{row-14}')
    fill_cell(row + 25, i, 'formula', '{extract_5.E}={extract_5.F}*{extract_5.H}')
  }
}

export function 抽汽6(row) {
  let data = [
    ['六段抽汽', '六段抽汽压力', 'MPa'], // +2
    ['', '六段抽汽温度', '℃'],
    ['', '六段抽汽焓值', 'kJ/kg'],
    ['', '6号低加进汽压力', 'MPa'], // +5
    ['', '6号低加进汽温度', '℃'],
    ['', '6号低加进汽焓值', 'kJ/kg'],
    ['', '凝泵出口压力', 'MPa'],
    ['', '6号低加进水压力', 'MPa'], //+8
    ['', '6号低加进水温度', '℃'],
    ['', '6号低加进水焓值', 'kJ/kg'],
    ['', '6号低加出水压力', 'MPa'], //+11
    ['', '6号低加出水温度', '℃'],
    ['', '6号低加出水焓值', 'kJ/kg'],
    ['', '6号低加疏水温度', '℃'], //+14
    ['', '6号低加疏水焓值', 'kJ/kg'],
    ['', '六段抽汽流量', 't/h'], //+16
    ['', '六号低加进汽流量', 't/h'],
    ['', '六号低加疏水流量', 't/h'],
    ['', '流经6号低加的流量', 't/h'],
    ['', '六段抽汽压损', '%'],
    ['', '上端差', '℃'],
    ['', '下端差', '℃'],
    ['', '温升', '℃'],
    ['', '六段抽汽能量', 'MJ/h']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  let cols = getLastColumnIndexOfSheet('数据整理')
  for (let i = 4; i <= cols; i++) {
    fill_cell(row + 2, i, 'para', 'extract_6.P')
    fill_cell(row + 3, i, 'para', 'extract_6.T')
    fill_cell(row + 4, i, 'formula', '{extract_6.H}=if97.H_PT({row-2}, {row-1})')
    // ------------------------------- 低加进汽 ---------------------------------
    fill_cell(row + 5, i, 'para', 'heater_6_vapor_in.P')
    fill_cell(row + 6, i, 'para', 'heater_6_vapor_in.T')
    fill_cell(row + 7, i, 'formula', '{heater_6_vapor_in.H}=if97.H_PT({row-2},{row-1})')
    // =============================== 低加进汽 ====================================
    // ----------------------------低加进水-----------------------------------
    // 查询凝泵出口压力测点，依次选取凝泵出口压力，凝泵A出口压力、凝泵B出口压力、主凝结水压力
    fill_cell(row + 8, i, 'formula', '{water_condense_pump_out.P}={water_condense_pump_out.P}')
    let 低加级数 = JSON.parse(wps.PluginStorage.getItem('低加级数'))  // 此处低加级数必然已经初始化
    if (低加级数) {
      低加级数 = 低加级数.length
    } else {
      alert('低加级数未知！')
    }
    fill_cell(row + 9, i, 'formula', '{heater_6_water_in.P}={deaerator_water_in.P}' +
      '+2*({row-1}-{deaerator_water_in.P})/' + 低加级数)
    fill_cell(row + 10, i, 'para', 'heater_6_water_in.T')
    fill_cell(row + 11, i, 'formula', '{heater_6_water_in.H}=if97.H_PT({row-2},{row-1})')
    // ============================低加进水压力=====================================
    // ------------------------------------ 高加出水 -----------------------------------------
    fill_cell(row + 12, i, 'para', 'heater_6_water_out.P')
    fill_cell(row + 13, i, 'para', 'heater_6_water_out.T')
    fill_cell(row + 14, i, 'formula', '{heater_6_water_out.H}=if97.H_PT({row-2},{row-1})')
    // =============================== 高加出水 =================================
    // -------------------------------- 疏水 -------------------------------------
    fill_cell(row + 15, i, 'para', 'heater_6_vapor_out.T')
    fill_cell(row + 16, i, 'formula', '{heater_6_vapor_out.H}=HW_T({row-1})')
    // ============================疏水=====================================
    // 6段抽汽流量
    fill_cell(row + 17, i, 'formula', '{extract_6.F}=({row+3}*({row-3}-{row-6})-{heater_5_vapor_out.F}*({heater_5_vapor_out.H}-{row-1}))/({row-10}-{row-1})') // 抽汽流量
    fill_cell(row + 18, i, 'formula', '{heater_6_vapor_in.F}={row-1}')// 进汽流量
    // 疏水流量
    let sp = Enum.StandardPoint['heater_6']
    if (sp['drain_to'] === 'heater_7') { // 如果6号低加的疏水流入7号低加
      fill_cell(row + 19, i, 'formula', '{heater_6_vapor_out.F}={row-1}+{heater_5_vapor_out.F}')
      // 流经高加的给水流量
      fill_cell(row + 20, i, 'formula', '{heater_6.F}={heater_5.F}')
      fill_cell(row + 21, i, 'formula', '{extract_6.压损}=({row-19}-{row-16})/{row-19}*100')
      fill_cell(row + 22, i, 'formula', '{heater_6.上端差}=T_P({row-17})-{row-9}')
      fill_cell(row + 23, i, 'formula', '{heater_6.下端差}={row-8}-{row-13}')
      fill_cell(row + 24, i, 'formula', '{heater_6.温升}={row-11}-{row-14}')
    }
    fill_cell(row + 25, i, 'formula', '{extract_6.E}={extract_6.F}*{extract_6.H}')
  }
}

export function 抽汽7(row) {
  // 判断7抽是否是湿蒸汽，湿蒸汽则不计算
  let 低加级数 = JSON.parse(wps.PluginStorage.getItem('低加级数'))
  if (!lodash.includes(低加级数, 9)) {
    // 如果没有9号低加，则7端抽汽往往是湿蒸汽，不参与计算
    return
  }
  let data = [
    ['七段抽汽', '七段抽汽压力', 'MPa'], // +2
    ['', '七段抽汽温度', '℃'],
    ['', '七段抽汽焓值', 'kJ/kg'],
    ['', '7号低加进汽压力', 'MPa'], // +5
    ['', '7号低加进汽温度', '℃'],
    ['', '7号低加进汽焓值', 'kJ/kg'],
    ['', '凝泵出口压力', 'MPa'],
    ['', '7号低加进水压力', 'MPa'], //+8
    ['', '7号低加进水温度', '℃'],
    ['', '7号低加进水焓值', 'kJ/kg'],
    ['', '7号低加出水压力', 'MPa'], //+11
    ['', '7号低加出水温度', '℃'],
    ['', '7号低加出水焓值', 'kJ/kg'],
    ['', '7号低加疏水温度', '℃'], //+14
    ['', '7号低加疏水焓值', 'kJ/kg'],
    ['', '七段抽汽流量', 't/h'], //+16
    ['', '七号低加进汽流量', 't/h'],
    ['', '七号低加疏水流量', 't/h'],
    ['', '流经7号低加的流量', 't/h'],
    ['', '七段抽汽压损', '%'],
    ['', '上端差', '℃'],
    ['', '下端差', '℃'],
    ['', '温升', '℃'],
    ['', '七段抽汽能量', 'MJ/h']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  let cols = getLastColumnIndexOfSheet('数据整理')
  for (let i = 4; i <= cols; i++) {
    fill_cell(row + 2, i, 'para', 'extract_7.P')
    fill_cell(row + 3, i, 'para', 'extract_7.T')
    fill_cell(row + 4, i, 'formula', '{extract_7.H}=if97.H_PT({row-2}, {row-1})')
    // ------------------------------- 低加进汽 ---------------------------------
    fill_cell(row + 5, i, 'para', 'heater_7_vapor_in.P')
    fill_cell(row + 6, i, 'para', 'heater_7_vapor_in.T')
    fill_cell(row + 7, i, 'formula', '{heater_7_vapor_in.H}=if97.H_PT({row-2},{row-1})')
    // =============================== 低加进汽 ====================================
    // ----------------------------低加进水-----------------------------------
    // 查询凝泵出口压力测点，依次选取凝泵出口压力，凝泵A出口压力、凝泵B出口压力、主凝结水压力
    fill_cell(row + 8, i, 'para', 'water_condense_pump_out.P')
    let 低加级数 = JSON.parse(wps.PluginStorage.getItem('低加级数'))  // 此处低加级数必然已经初始化
    if (低加级数) {
      低加级数 = 低加级数.length
    } else {
      alert('低加级数未知！')
    }
    fill_cell(row + 9, i, 'formula', '{heater_7_water_in.P}={deaerator_water_in.P}' +
      '+3*({row-1}-{deaerator_water_in.P})/' + 低加级数)
    fill_cell(row + 10, i, 'para', 'heater_7_water_in.T')
    fill_cell(row + 11, i, 'formula', '{heater_7_water_in.H}=if97.H_PT({row-2},{row-1})')
    // ============================低加进水压力=====================================
    // ------------------------------------ 高加出水 -----------------------------------------
    fill_cell(row + 12, i, 'para', 'heater_7_water_out.P')
    fill_cell(row + 13, i, 'para', 'heater_7_water_out.T')
    fill_cell(row + 14, i, 'formula', '{heater_7_water_out.H}=if97.H_PT({row-2},{row-1})')
    // =============================== 高加出水 =================================
    // -------------------------------- 疏水 -------------------------------------
    fill_cell(row + 15, i, 'para', 'heater_7_vapor_out.T')
    fill_cell(row + 16, i, 'formula', '{heater_7_vapor_out.H}=HW_T({row-1})')
    // ============================疏水=====================================
    // 疏水流量
    let sp = Enum.StandardPoint['heater_7']
    if (sp['drain_to'] === 'heater_8') { // 如果7号低加的疏水流入8号低加
      // 7段抽汽流量
      fill_cell(row + 17, i, 'formula', '{extract_7.F}=({row+3}*({row-3}-{row-6})-{heater_6_vapor_out.F}*({heater_6_vapor_out.H}-{row-1}))/({row-10}-{row-1})') // 抽汽流量
      fill_cell(row + 18, i, 'formula', '{heater_7_vapor_in.F}={row-1}')// 进汽流量
      if (Enum.StandardPoint['heater_6']['drain_to'] === 'heater_7') { // 如果上一级疏水进入本级
        fill_cell(row + 19, i, 'formula', '{heater_7_vapor_out.F}={row-1}+{heater_6_vapor_out.F}') // 疏水流量
      } else {
        fill_cell(row + 19, i, 'formula', '{heater_7_vapor_out.F}={row-1}')// 疏水流量
      }
      // 流经高加的给水流量
      fill_cell(row + 20, i, 'formula', '{heater_7.F}={heater_6.F}')
      fill_cell(row + 21, i, 'formula', '{extract_7.压损}=({row-19}-{row-16})/{row-19}*100')
      fill_cell(row + 22, i, 'formula', '{heater_7.上端差}=T_P({row-17})-{row-9}')
      fill_cell(row + 23, i, 'formula', '{heater_7.下端差}={row-8}-{row-13}')
      fill_cell(row + 24, i, 'formula', '{heater_7.温升}={row-11}-{row-14}')
    } else if (sp['drain_to'] === 'heater_6_water_in') { // 如果7号低加疏水打入6号低加入口
      // 7段抽汽流量
      fill_cell(row + 17, i, 'formula', '{extract_7.F}={row+1}') // 抽汽流量
      fill_cell(row + 18, i, 'formula', '{heater_7_vapor_in.F}=(({heater_6.F}-{heater_6_vapor_out.F})*({heater_7_water_out.H}-{heater_7_water_in.H})-{heater_6_vapor_out.F}*({heater_6_vapor_out.H}-{heater_7_vapor_out.H}))/({heater_7_vapor_in.H}-{heater_7_vapor_out.H}+{heater_7_water_out.H}-{heater_7_water_in.H})')// 进汽流量
      if (Enum.StandardPoint['heater_6']['drain_to'] === 'heater_7') {
        fill_cell(row + 19, i, 'formula', '{heater_7_vapor_out.F}={row-1}+{heater_6_vapor_out.F}') // 疏水流量
      } else {
        fill_cell(row + 19, i, 'formula', '{heater_7_vapor_out.F}={row-1}')// 疏水流量
      }
      // 流经高加的给水流量
      fill_cell(row + 20, i, 'formula', '{heater_7.F}={heater_6.F}-{row-1}')
      fill_cell(row + 21, i, 'formula', '{extract_7.压损}=({row-19}-{row-16})/{row-19}*100')
      fill_cell(row + 22, i, 'formula', '{heater_7.上端差}=T_P({row-17})-{row-9}')
      fill_cell(row + 23, i, 'formula', '{heater_7.下端差}={row-8}-{row-13}')
      fill_cell(row + 24, i, 'formula', '{heater_7.温升}={row-11}-{row-14}')
    }
    fill_cell(row + 25, i, 'formula', '{extract_7.E}={extract_7.F}*{extract_7.H}')
  }
}

export function 高压缸排汽(row) {
  let data = [
    ['高压缸级', '高压缸排汽压力', 'MPa'],
    ['', '高压缸排汽温度', '℃'],
    ['', '高压缸排汽焓值', 'kJ/kg'],
    ['', '高压缸排汽流量', 't/h'],
    ['', '冷段再热蒸汽焓值', 'kJ/kg'],
    ['', '冷段再热蒸汽流量', 't/h'],
    ['', '主汽至一段蒸汽流量', 't/h'],
    ['', '主汽至一抽段功率', 'MW'],
    ['', '一抽至二抽段蒸汽流量', 't/h'],
    ['', '一抽至二抽段功率', 'MW'],
    ['', '高压缸轴功率', 'MW']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  for (let i = 4; i <= getLastColumnIndexOfSheet("数据整理"); i++) {
    fill_cell(row + 2, i, 'para', 'outlet_high.P')
    fill_cell(row + 3, i, 'para', 'outlet_high.T')
    fill_cell(row + 4, i, 'formula', '{outlet_high.H}=if97.H_PT({row-2}, {row-1})')
    fill_cell(row + 5, i, 'para', '{outlet_high.F}') // 高排流量
    fill_cell(row + 6, i, 'formula', '{cold_reheat_steam.H}={row-2}') // 冷再焓值
    fill_cell(row + 7, i, 'para', '{cold_reheat_steam.F}')
    fill_cell(row + 8, i, 'formula', '{turbine_high.主汽至一抽段流量}={main_steam.F}-' +
      '{leakage.高压前轴封漏汽量}-{leakage.高压门杆漏汽量}')
    fill_cell(row + 9, i, 'formula', '{turbine_high.主汽至一抽段功率}={row-1}*({main_steam.H}-{extract_1.H})/3600')
    fill_cell(row + 10, i, 'formula', '{turbine_high.一抽至高排段流量}={row-2}-{extract_1.F}')
    fill_cell(row + 11, i, 'formula', '{turbine_high.一抽至高排段功率}={row-1}*({extract_1.H}-{row-7})/3600')
    fill_cell(row + 12, i, 'formula', '{turbine_high.shaft_power}={row-1}+{row-3}')
  }
}

export function 再热蒸汽(row) {
  let data = [
    ['中压缸级', '再热蒸汽压力', 'MPa'],
    ['', '再热蒸汽温度', '℃'],
    ['', '再热蒸汽焓', 'kJ/kg'],
    ['', '再热蒸汽流量', 't/h'],
    ['', '再热器压损', '%'],
    ['', '中压缸进汽至三抽段流量', 't/h'],
    ['', '中压缸进汽至三抽段功率', 'MW'],
    ['', '三抽至四抽段流量', 't/h'],
    ['', '三抽至四抽段功率', 'MW'],
    ['', '四抽至中排段流量', 't/h'],
    ['', '四抽至中排段功率', 'MW'],
    ['', '中压缸轴功率', 'MW']
  ]
  set_cells(row + 2, 1, data)
  set_cell_color(row + 2, 1, 4) // 设置第一个单元格背景色
  app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true) //滚动窗口到更改内容
  for (let i = 4; i <= getLastColumnIndexOfSheet("数据整理"); i++) {
    fill_cell(row + 2, i, 'para', 'inlet_medium.P')
    fill_cell(row + 3, i, 'para', 'inlet_medium.T')
    fill_cell(row + 4, i, 'formula', '{inlet_medium.H}=if97.H_PT({row-2}, {row-1})')
    fill_cell(row + 5, i, 'formula', '{inlet_medium.F}={cold_reheat_steam.F}+' +
      '{water_reheater_reducing.F}')
    fill_cell(row + 6, i, 'formula', '{reheater.压损}=({cold_reheat_steam.P}-{row-4})/{cold_reheat_steam.P}*100') // 冷再焓值
    fill_cell(row + 7, i, 'formula', '{turbine_medium.中进至三抽段流量}={row-2}-{leakage.中压前轴封漏汽量}')
    fill_cell(row + 8, i, 'formula', '{turbine_medium.中进至三抽段功率}={row-1}*({row-4}-{extract_3.H})/3600')
    fill_cell(row + 9, i, 'formula', '{turbine_medium.三抽至四抽段流量}={row-2}-{extract_3.F}')
    fill_cell(row + 10, i, 'formula', '{turbine_medium.三抽至四抽段功率}={row-1}*({extract_3.H}-{extract_4.H})/3600')
    fill_cell(row + 11, i, 'formula', '{turbine_medium.四抽至中排段流量}={row-2}-{extract_4.F}')
    fill_cell(row + 12, i, 'formula', '{turbine_medium.四抽至中排段功率}={row-1}*({extract_4.H}-{outlet_medium.H})/3600')
    fill_cell(row + 13, i, 'formula', '{turbine_medium.shaft_power}={turbine_medium.中进至三抽段功率}+{turbine_medium.三抽至四抽段功率}+{turbine_medium.四抽至中排段功率}')
  }
}

export function correct_gas_turbine(row) {
  /**
   * 联合循环性能修正
   *
   */
    // 获取当前单元格对应的参数
  let active_sheet_name = get_active_sheet_name()
  let origin_data_sheet = get_sheet_by_name('原始数据')
  let test_data_sheet = get_sheet_by_name('数据整理')
  let _r = app.ActiveCell.Row
  let _c = app.ActiveCell.Column
  if (row === undefined || row === null) {
    row = _r
  }
  if (row <= 4) {
    alert(`只能自动填充第5行及以后的数据区域，当前选中的单元位于${row}`)
    return
  }
  if (lodash.includes(active_sheet_name, '原始数据') || lodash.includes(active_sheet_name, '数据整理')) {
    alert(`不能在【原始数据】表或【数据整理】表中添加联合循环修正计算模块`)
    return
  }
  let data = [
    ['修正', '', ''],
    ['', '试验环境温度', '℃'],
    ['', '设计环境温度', '℃'],
    ['', '环境温度变化量', '℃'],
    ['', '环境温度对功率修正系数', '/'],
    ['', '环境温度对热耗率修正系数', '/'],
    ['', '', ''],
    ['', '试验大气压力', 'kPa'],
    ['', '设计大气压力', 'kPa'],
    ['', '大气压力变化量', 'kPa'],
    ['', '大气压力对功率修正系数', '/'],
    ['', '大气压力对热耗率修正系数', '/'],
    ['', '', ''],
    ['', '试验相对湿度', '%'],
    ['', '设计相对湿度', '%'],
    ['', '相对湿度变化量', '%'],
    ['', '相对湿度对功率修正系数', '/'],
    ['', '相对湿度对热耗率修正系数', '/']
  ]
  set_cells(row, 1, data)
  set_cell_color(row, 1, 4) // 设置第一个单元格背景色
  // app.Goto(app.ActiveSheet.Cells.Item(row, 1), true); //滚动窗口到更改内容
  if (Enum.StandardPoint.curve_correct.href.f_T0_power === null || Enum.StandardPoint.curve_correct.href.f_T0_power.length === 0) {
    wps.ShowDialog(Util.GetUrlPath() + 'correctCurve/f_T0_power', '修正曲线设置', 1200, 800)

  }
  for (let i = 4; i <= getLastColumnIndexOfSheet("数据整理"); i++) {
    fill_cell(row + 1, i, 'para', 'env.T')
    fill_cell(row + 2, i, 'para', 'env.T')
    fill_cell(row + 3, i, 'formula', '{correct_factor.delta_T0}={row-2}-{row-1}')
    fill_cell(row + 4, i, 'formula', '{correct_factor.f_T0_power}=插值_1维({row-1}, {curve_corrector.f_T0_power})')
    fill_cell(row + 5, i, 'formula', '{correct_factor.f_T0_hr}=插值_1维({row-1}, {curve_corrector.f_T0_hr})')
  }
}


export function fill_data_from_origin(row) {
  /**
   * 从原始数据工作表中引用数据至当前单元格所代表的参数
   * @type {string[][]}
   */
    // 获取当前单元格对应的参数
  let active_sheet_name = get_active_sheet_name()
  let origin_data_sheet = get_sheet_by_name('原始数据')
  let test_data_sheet = get_sheet_by_name('数据整理')
  let _r = app.ActiveCell.Row
  let _c = app.ActiveCell.Column
  if (_c <= 3) {
    alert(`只能自动填充D列及以后的数据区域，当前选中的单元位于${Systemdemo.Num2ColumnChar(_c)}`)
    return
  }
  let find_var = get_cell_value(_r, 2, active_sheet_name)
  let sp, pos
  if (lodash.includes(active_sheet_name, '原始数据')) {
    alert('\'原始数据\'表中的数据来自试验采集/DCS导出/手工抄录，无法自动填充！')
    return
  } else if (active_sheet_name === '数据整理') {
    if (origin_data_sheet === null) {
      return null
    }
    Systemdemo.refreshOriginPoints()
    // 至此，eu.StandardPointOrigin中储存了所有参数的位置
    sp = Enum.StandardPointOrigin
  } else {
    if (test_data_sheet === null) {
      return null
    }
    Systemdemo.refreshTestPoints()
    // 至此，eu.StandardPoint中储存了所有参数的位置
    sp = Enum.StandardPoint
  }
  make_equals(sp)

  let para: any = Systemdemo.dispatch_point_to_para(find_var, sp)
  let _para = lodash.clone(para)
  if (para === null) {
    alert(`当前测点名称${find_var}无法识别为任何已知测点！`)
    return null
  } else {
    para = `${para[0]}.${para[1]}`
    pos = systemdemo.get_position_of_para(para, _c, false, sp, true)
  }
  let current_cell_pos_string = [`${active_sheet_name}!${Systemdemo.Num2ColumnChar(_c)}${_r}`, `${Systemdemo.Num2ColumnChar(_c)}${_r}`]
  let row_1, row_2, row_3
  if (pos === null || lodash.includes(current_cell_pos_string, pos)) {
    if (_para[1] === 'H') {
      /**
       * 如果待填充的位置是焓值测点，且未查找到焓值引用，则判断待填充位置前几行是否有压力和温度，有则自动计算焓值。
       * 如果当前焓值引用的是当前位置，也当做未找到引用处理
       */
      // 判断当前填充行上几行的参数是否是压力和温度值
      row_1 = get_cell_value(_r - 1, 2, active_sheet_name)
      row_2 = get_cell_value(_r - 2, 2, active_sheet_name)
      row_3 = get_cell_value(_r - 3, 2, active_sheet_name)
      if (lodash.includes(row_2, '压力') && lodash.includes(row_1, '温度')) {
        fill_cell(_r, _c, 'formula', `{${_para[0]}.H}=if97.H_PT({row-2}, {row-1})`, null, sp)
        return
      } else if (lodash.includes(row_3, '压力') && lodash.includes(row_2, '温度')) {
        fill_cell(_r, _c, 'formula', `{${_para[0]}.H}=if97.H_PT({row-3}, {row-2})`, null, sp)
        return
      }
    } else if (_para[1] === 'S') {
      /**
       * 如果待填充的位置是熵值测点，且未查找到熵值引用，则判断待填充位置前几行是否有压力和温度，有则自动计算熵值。
       * 如果当前熵值引用的是当前位置，也当做未找到引用处理
       */
      // 判断当前填充行上几行的参数是否是压力和温度值
      row_1 = get_cell_value(_r - 1, 2, active_sheet_name)
      row_2 = get_cell_value(_r - 2, 2, active_sheet_name)
      row_3 = get_cell_value(_r - 3, 2, active_sheet_name)
      if (lodash.includes(row_2, '压力') && lodash.includes(row_1, '温度')) {
        fill_cell(_r, _c, 'formula', `{${_para[0]}.S}=if97.S_PT({row-2}, {row-1})`, null, sp)
        return
      } else if (lodash.includes(row_3, '压力') && lodash.includes(row_2, '温度')) {
        fill_cell(_r, _c, 'formula', `{${_para[0]}.S}=if97.S_PT({row-3}, {row-2})`, null, sp)
        return
      }
    }
    // wps.ShowDialog("/", "未找到测点链接！")
    alert('目标sheet中未找到相应测点链接！')
    return null
  }

  if (lodash.isString(pos)) {  // 说明pos是单个位置字符串，否则pos可能是多个位置字符串的列表
    fill_cell(_r, _c, 'para', para, null, sp)
  } else if (lodash.isArray(pos) && pos.length === 1) {
    fill_cell(_r, _c, 'para', para, null, sp)
  } else {
    // 判断多个pos位置是否相邻
    let adjacent = true
    let rows = []
    let formula_slice: any = []
    for (let p of pos) {
      let r = Systemdemo.get_row_from_pos_string(p)
      if (rows.length === 0) {
        rows.push(r)
        formula_slice.push(p)
      } else {
        if (r - rows[rows.length - 1] === 1) {  // 如果两个参数行号相邻
          // 继续下一循环
          rows.push(r)
          formula_slice.push(p)
        } else {
          adjacent = false  // 行号不相邻，一般为同一个测点来源不同的参数，如试验仪表和运行仪表测量的同一个参数
          break
        }
      }
    }

    if (adjacent) { // 如果多个pos相邻，则直接取平均值
      formula_slice = formula_slice.join(',')
      fill_cell(_r, _c, 'formula', `=AVERAGE(${formula_slice})`)
    } else {  // 如果不相邻，则弹出面板，让用户决定取哪个参数的值
      fill_cell(_r, _c, 'formula', `=AVERAGE(${formula_slice})`)
      let _ = Systemdemo.get_cell_value_by_pos('原始数据!E12')
      // 弹出面板，面板上可以选择需要的测点取平均值
      Systemdemo.showTaskPane(`testPointInfo/${JSON.stringify(pos)}`, '链接测点', true)  // 将pos作为参数传递给页面
    }

  }
}